package com.google.code.mochaccino.framework.jmx;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ReflectionException;
import java.io.IOException;

/**
 * A JMX Method Invocation
 *
 * @param <T>
 *
 * @author choule
 */
public class JmxValueMethod<T> implements JmxValue<T> {

	private JmxBean       bean       = null;
	private boolean       cacheData  = true;
	private Object        cachedData = null;
	private JmxConnection connection = null;
	private Logger        logger     = LoggerFactory.getLogger( getClass() );
	private String        methodName = null;
	private String        name       = null;
	private Object[]      parameters = null;
	private String[]      signature  = null;

	public JmxValueMethod() {
	}

	public JmxBean getBean() {
		return bean;
	}

	public void setBean( JmxBean name ) {
		this.bean = name;
	}

	public JmxConnection getConnection() {
		return connection;
	}

	public void setConnection( JmxConnection connection ) {
		this.connection = connection;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName( String methodName ) {
		this.methodName = methodName;
	}

	/**
	 * @return the name
	 */
	@Override
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName( String name ) {
		this.name = name;
	}

	public Object[] getParameters() {
		return parameters;
	}

	public void setParameters( Object[] parameters ) {
		this.parameters = parameters;
	}

	public String[] getSignature() {
		return signature;
	}

	public void setSignature( String[] signature ) {
		this.signature = signature;
	}

	@SuppressWarnings( "unchecked" )
	@Override
	public T getValue() {
		if ( (cachedData != null) && cacheData ) {
			return (T) cachedData;
		}
		cachedData = rawInvoke();
		return (T) cachedData;
	}

	protected Object rawInvoke() {
		try {
			MBeanServerConnection conn = connection.getConnection();
			if ( conn != null ) {
				return conn.invoke( bean.getObjectName(), methodName, parameters, signature );
			}
		} catch ( InstanceNotFoundException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( MBeanException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( ReflectionException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
			return null;
		} catch ( IOException e ) {
			if ( logger.isErrorEnabled() ) {
				logger.error( e.getMessage(), e );
			}
		}
		return null;
	}

	public boolean isCacheData() {
		return cacheData;
	}

	public void setCacheData( boolean cacheData ) {
		this.cacheData = cacheData;
	}
}
